package com.mpgame.service;

import com.mpgame.common.proto.*;
import com.mpgame.common.utils.Log;

import io.grpc.stub.StreamObserver;

public class ChatSvc extends ChatServerServiceGrpc.ChatServerServiceImplBase{
    @Override
    public void sendMsg(ChatReq req, StreamObserver<ChatRspBroadcast> rspObs){
        String content = req.getContent();
        if(content == null || content.isBlank()){
            rspObs.onNext(ChatRspBroadcast
                            .newBuilder()
                            .setErrMsg("内容不能为空！")
                            .build()
                        );
            rspObs.onCompleted();
            return;
        }
        Log.info("收到聊天消息：" + content);
        ChatRspBroadcast rsp = ChatRspBroadcast.newBuilder()
            .setRsp(ChatRsp.newBuilder()
                .setSenderName(req.getSenderName())
                .setContent(req.getContent())
                .build())
            .addTargetPlayers(-1)
            .build();
        rspObs.onNext(rsp);
        rspObs.onCompleted();
    }
}
